<template>
  <div class="container">
    <my-card>
      <template slot>
        <div class="enterprise-edit-page">
          <el-form ref="form" :model="formData" label-width="120px">
            <el-form-item>
              <div class="item-title-box">基本信息</div>
            </el-form-item>
            <el-form-item label="用户名：">
              <el-input v-model="formData.account" disabled></el-input>
            </el-form-item>
            <el-form-item label="公司名称：">
              <el-input v-model="formData.companyName" disabled></el-input>
            </el-form-item>
            <el-form-item label="官网名称：">
              <el-input v-model="formData.officialNetName" disabled></el-input>
            </el-form-item>
            <el-form-item label="企业官网地址：">
              <el-input v-model="formData.officialNet" disabled></el-input>
            </el-form-item>
            <el-form-item>
              <div class="item-title-box">详细信息</div>
            </el-form-item>
            <el-form-item label="所在省市：">
              <el-input v-model="formData.location"></el-input>
            </el-form-item>
            <el-form-item label="联系人：">
              <el-input v-model="formData.name"></el-input>
            </el-form-item>
            <el-form-item label="联系地址：">
              <el-input v-model="formData.address"></el-input>
            </el-form-item>
            <el-form-item label="手机号码：">
              <el-input v-model="formData.phone"></el-input>
            </el-form-item>
            <el-form-item label="邮箱地址：">
              <el-input v-model="formData.email"></el-input>
            </el-form-item>
            <el-form-item>
              <div class="item-title-box">发票邮寄地址</div>
            </el-form-item>
            <el-form-item label="收件人：">
              <el-input v-model="formData.iReceiver"></el-input>
            </el-form-item>
            <el-form-item label="邮寄地址：">
              <el-input v-model="formData.iAddress"></el-input>
            </el-form-item>
            <el-form-item label="手机号码：">
              <el-input v-model="formData.iPhone"></el-input>
            </el-form-item>
            <el-form-item label="邮政编码：">
              <el-input v-model="formData.iPostCode"></el-input>
            </el-form-item>
            <el-form-item>
              <div class="bottom-btn-box">
                <el-button type="primary" plain @click="hkSubmit">
                  保存
                </el-button>
                <el-button type="success" plain @click="hkClickEditPwd">
                  修改密码
                </el-button>
                <el-button @click="hkClickBack">返回</el-button>
              </div>
            </el-form-item>
          </el-form>
        </div>
      </template>
    </my-card>
  </div>
</template>

<script>
import MyCard from "@/components/my-card/index";
import {
  hkHidLoading,
  hkShowErrorText,
  hkShowLoading,
  hkShowSuccessText,
  isNull
} from "@/utils/tools";
import { hkPut } from "@/api/http";
import { httpApi } from "@/api/api";
export default {
  components: { MyCard },
  props: {},
  data() {
    return {
      cacheInfo: {},
      formData: {
        location: "",
        name: "", // 用户姓名
        address: "", // 用户联系地址
        phone: "", // 电话号码,需唯一
        email: "", // 用户的邮箱
        iReceiver: "", // 企业用户邮件接收人
        iAddress: "", // 企业用户邮件接收地址
        iPhone: "", // 企业用户邮件接收联系方式
        iPostCode: "" // 企业用户邮件接收邮政编码
      }
    };
  },
  watch: {},
  computed: {},
  methods: {
    hkSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          let params = {};
          for (let key in this.formData) {
            if (this.formData[key] != this.cacheInfo[key]) {
              params[key] = this.formData[key];
            }
          }
          if (isNull(params)) {
            return;
          }
          params.id = this.cacheInfo.id;
          hkShowLoading("修改中....");
          hkPut(httpApi.userBasic, params, false)
            .then(res => {
              hkHidLoading();
              hkShowSuccessText("修改成功!");
              setTimeout(() => {
                this.$router.back();
              }, 1500);
            })
            .catch(err => {
              hkHidLoading();
              hkShowErrorText(err.message);
            });
        } else {

          return false;
        }
      });
    },
    hkClickBack() {
      this.$router.back();
    },
    hkClickEditPwd() {
      this.$router.push({
        path: "/account/ent/pwd",
        query: { id: this.cacheInfo.id }
      });
    }
  },
  created() {},
  mounted() {
    let query = this.$route.query;
    if (isNull(query) && isNull(query.info)) {
      return;
    }
    this.cacheInfo = JSON.parse(decodeURIComponent(query.info));
    for (const key in this.cacheInfo) {
      if (Object.hasOwnProperty.call(this.cacheInfo, key)) {
        this.formData[key] = this.cacheInfo[key];
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.enterprise-edit-page {
  width: 700px;
  margin: 0 auto;
  .item-title-box {
    text-align: center;
  }
  .bottom-btn-box {
    text-align: center;
  }
}
</style>
